package com.vanzo.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import com.vanzo.Tpplication;
import com.vanzo.instruction.AT;
import com.vanzo.instruction.Instruction;
import com.vanzo.util.LogUtil;

/**
 * Created by tisen on 2018/2/23.
 */

public class MessengerService extends Service {

    public static final String TAG = "MessengerService";
    private Messenger client;

    static final int MSG_PTT_DOWN = 1;
    static final int MSG_PTT_UP = 2;

    class InComingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            LogUtil.put(TAG, "msg = " + msg.toString(), LogUtil.WARN);
            client = msg.replyTo;
            switch (msg.what) {
                case MSG_PTT_DOWN:
                    instruction.sendInstruction(AT.DMO_PTT+"=1");
                    break;
                case MSG_PTT_UP:
                    instruction.sendInstruction(AT.DMO_PTT+"=0");
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }



    final Messenger messenger = new Messenger(new InComingHandler());

    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.put(this, "onBind", LogUtil.WARN);
        return messenger.getBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtil.put(this, "onUnbind", LogUtil.WARN);
        return super.onUnbind(intent);
    }

    private Instruction instruction;

    @Override
    public void onCreate() {
        super.onCreate();
        instruction = ((Tpplication)getApplication()).getInstruction();
//        instruction.interrupt();
//        instruction.reStart();
        LogUtil.put(this, "onCreate", LogUtil.WARN);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.put(this, "onStartCommand", LogUtil.WARN);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.put(this, "onDestroy", LogUtil.WARN);
    }
}
